package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.util.Log;

import java.util.ArrayList;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPillSingleBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updatePatientInfo.UpdateHospitalStaticNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalPillProgressViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/9/1.
 */

public class InHospitalPillProgressPresenter extends BasePresenter<InHospitalPillProgressViewModel> {
    ArrayList<InHospitalPillSingleBeen> FitstTotal;
    //    private ArrayList<InHospitalPillSingleBeen> mTestBeen=new ArrayList<>();
    private ApiService mApiService;
    private MDialog waitDialog;
    public String DJJLID;
    @Override
    public void onCreatePresenter() {
        loadData(false);
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
    }

    @Override
    public void loadData(boolean ref) {
        FitstTotal = (ArrayList<InHospitalPillSingleBeen>) activity.getIntent().getExtras().getSerializable("total");
        DJJLID=activity.getIntent().getExtras().getString("DJJLID");
//        Log.e("PXY", "InHospitalPillBeenList: "+FitstTotal.toString() );
        if(FitstTotal!=null ){
            viewModel.show(FitstTotal);
        }
    }
//    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
//    public boolean confim(String YPSPBM){
////        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
//        if(viewModel.remove != null ){
//            if(viewModel.remove.getYPSPBM().equals(YPSPBM)){
//                return true;
//            }
//        }
//        return false;
//    }
    //TODO:更新药单  药单已完成  PDAU04
//    DJJLID	处方号	Varchar2(20)
//    DJDQZT	单据当前状态 	Number(1)
//    CZYHID	操作用户ID	Varchar2(20)
    public  void  update(String DJDQZT,String CZYHID){
        Disposable disposable = null;
        boolean result;
        disposable = mApiService.doXml(XmlUtils.prepare(UpdateHospitalStaticNode.getCheckContent("PDAU04",DJJLID,DJDQZT,CZYHID,"4")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {

                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
//                        waitDialog.dismiss();
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
                            mOnSubmitResult.submitFail(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult);
                            return;
                        }
                        mOnSubmitResult.submitSuccess();
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }

    //药单更新监听
    private OnSubmitHospitalResult mOnSubmitResult;

    public void registeOnSubmitResultListener(OnSubmitHospitalResult mOnScanResult) {
        this.mOnSubmitResult = mOnScanResult;
    }

    public void unRegisteOnSubmitResultListener() {
        this.mOnSubmitResult = null;
    }
    //药单更新监听
    public interface OnSubmitHospitalResult {
        void submitSuccess();
        void submitFail(String errMsg);
    }
}
